package com.taoyibao.hongcanting.ui.login.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.base.activity.BaseActivityPresenter;
import com.taoyibao.hongcanting.cache.SpUtils;
import com.taoyibao.hongcanting.constant.CodeConstant;
import com.taoyibao.hongcanting.ui.MainActivity;
import com.taoyibao.hongcanting.ui.admin.activity.AdministratorActivity;
import com.taoyibao.hongcanting.ui.admin.activity.WaiterActivity;
import com.taoyibao.hongcanting.ui.login.delegate.SplashDelegate;

/**
 * @author TangAnna
 * @description: 闪屏页面
 * @date :${DATA} 15:16
 */
public class SplashActivity extends BaseActivityPresenter<SplashDelegate> implements View.OnClickListener {

    private TextView mTvTime;
    private int time = 3;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                onNext();
            } else if (msg.what == 0) {
                time--;
                mTvTime.setText(time + "s 跳过");
            }

        }
    };

    @Override
    protected Class<SplashDelegate> getDelegateClass() {
        return SplashDelegate.class;
    }

    @Override
    protected void bindEvenListener() {
        super.bindEvenListener();
        mTvTime = viewDelegate.get(R.id.tv_splash_time);
        mTvTime.setText(time + "s 跳过");
        mHandler.sendEmptyMessageDelayed(0, 1000);
        mHandler.sendEmptyMessageDelayed(0, 2000);
        mHandler.sendEmptyMessageDelayed(1, 3000);

        mTvTime.setOnClickListener(this);
    }

    /**
     * 页面跳转
     */
    public void onNext() {
        if (SpUtils.getWelcomeStatus()) {
            startActivity(new Intent(this, WelcomeActivity.class));
        } else {
            if (SpUtils.getUserType() == CodeConstant.USER_TYPE_USER) {//普通用户
                startActivity(new Intent(this, MainActivity.class));
            } else if (SpUtils.getUserType() == CodeConstant.USER_TYPE_SCREEN_OPERATOR) {//屏幕操作员
                startActivity(new Intent(this, AdministratorActivity.class));
            } else if (SpUtils.getUserType() == CodeConstant.USER_TYPE_WAITER) {//服务员
                startActivity(new Intent(this, WaiterActivity.class));
            } else {
                startActivity(new Intent(this, MainActivity.class));
            }
        }
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHandler.removeMessages(0);
        mHandler.removeMessages(1);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_splash_time://跳过
                mHandler.removeMessages(0);
                mHandler.removeMessages(1);
                onNext();
                break;
        }
    }
}
